package com.wondersgroup.framework.widget.taglib.form;

import com.wondersgroup.framework.util.RandomUtils;
import com.wondersgroup.framework.widget.annotations.WidgetTag;
import com.wondersgroup.framework.widget.annotations.WidgetTagAttribute;
import com.wondersgroup.framework.widget.library.extjs.ExtComponent;
import com.wondersgroup.framework.widget.library.extjs.form.ExtTextField;
import com.wondersgroup.framework.widget.library.html.Div;
import com.wondersgroup.framework.widget.library.html.HtmlComponent;
import com.wondersgroup.framework.widget.taglib.form.util.FormUtils;
import com.wondersgroup.framework.widget.util.OutputUtils;
import com.wondersgroup.framework.widget.util.TagUtils;
import com.wondersgroup.framework.widget.validation.annotation.TypeValidate;
import javax.servlet.jsp.JspException;
import org.springframework.beans.BeanUtils;

@WidgetTag(name = "upload", tldBodyContent = "empty", tldTagClass = "com.wondersgroup.framework.widget.taglib.form.UploadTag", description = "当采用传统的html上传控件时遇见上传的文件过大，用户会经过漫长的等待放弃操作，使用该控件可以解决问题")
public class UploadTag extends RequiredTag {
	private int fileSize;
	private String allowFileType;
	private String allowFileTypeDesc = "请选择文件...";
	private int fileCount = 0;

	@TypeValidate
	private boolean enableMulti = true;
	private String url;
	private String urlParams;
	private String buttonImg;
	private String uploadImg;
	private String removeImg;
	private String height;
	private String width = "0";

	public void processStartTagContent() throws JspException {
		if (!(isEnableMulti())) {
			setFileCount(1);
		}
		String str1 = String.valueOf(RandomUtils.randInt(0, 999999));
		String str2 = getId() + "_" + str1;
		Div localDiv = new Div(str2 + "_div");
		ExtTextField localExtTextField = new ExtTextField(str2);
		BeanUtils.copyProperties(this, localExtTextField);
		localExtTextField.setId(str2);
		localExtTextField.setType("file");
		localExtTextField.setFormItems(getFormItems());
		if ((getToolBarArray() == null) && (getStatusBarArray() == null)) {
			localExtTextField.setRenderTo(localDiv.getId());
		}
			
		StringBuffer localStringBuffer1 = new StringBuffer();
		localStringBuffer1.append("<div id=\"").append(getId()).append("_div\">\n");
		localStringBuffer1.append("<span class=\"label_required\">").append(getAllowFileTypeDesc()).append("</span>\n");
		if (getFileCount() > 0) {
			localStringBuffer1.append("<span class=\"label_required\">最多").append(getFileCount()).append("个</span>\n");
		}
			
		localStringBuffer1.append(OutputUtils.writeHTML(new HtmlComponent[] { localDiv }));
		localStringBuffer1.append("</div>");
		String str3 = OutputUtils.ExtOnReady(new ExtComponent[] { localExtTextField });
		int i = "});</script>".length();
		StringBuffer localStringBuffer2 = new StringBuffer(str3.substring(0,
				str3.length() - i)).append("var div=document.getElementById('")
				.append(getId()).append("_div');");
		if (getAllowFileType() != null) {
			localStringBuffer2.append("div.setAttribute('allowFileType','").append(getAllowFileType()).append("');");
		}
			
		localStringBuffer2.append("div.setAttribute('fileSize',")
				.append(getFileSize())
				.append(");div.setAttribute('fileCount',")
				.append(getFileCount()).append(");");
		if (getButtonImg() != null) {
			localStringBuffer2.append("div.setAttribute('buttonImg','").append(getButtonImg()).append("');");
		}
			
		if (getRemoveImg() != null) {
			localStringBuffer2.append("div.setAttribute('removeImg','").append(getRemoveImg()).append("');");
		}
			
		localStringBuffer2
				.append("if(Ext.isIE){Ext.get('")
				.append(str2)
				.append("').dom.onchange = function(){Wonders.form.addFile('")
				.append(getId())
				.append("','")
				.append(str1)
				.append("')};}\n")
				.append("else{Ext.get('")
				.append(str2)
				.append("').dom.addEventListener('change',function(){Wonders.form.addFile('")
				.append(getId()).append("','").append(str1)
				.append("')},false);}").append("});</script>").toString();
		localStringBuffer1.append(localStringBuffer2.toString());
		FormUtils.addFieldsToForm(this.pageContext, localExtTextField);
		TagUtils.getInstance().write(this.pageContext, localStringBuffer1.toString());
	}

	public int getFileSize() {
		return this.fileSize;
	}

	public String getAllowFileType() {
		return this.allowFileType;
	}

	public String getAllowFileTypeDesc() {
		return this.allowFileTypeDesc;
	}

	public int getFileCount() {
		return this.fileCount;
	}

	public boolean isEnableMulti() {
		return this.enableMulti;
	}

	public String getUrl() {
		return this.url;
	}

	public String getUrlParams() {
		return this.urlParams;
	}

	public void setFileSize(int paramInt) {
		this.fileSize = paramInt;
	}

	@WidgetTagAttribute(description = "允许上传的文件类型，诸如*.jpg")
	public void setAllowFileType(String paramString) {
		this.allowFileType = paramString;
	}

	@WidgetTagAttribute(description = "允许上传的文件类型的描述")
	public void setAllowFileTypeDesc(String paramString) {
		this.allowFileTypeDesc = paramString;
	}

	@WidgetTagAttribute(description = "单次上传文件的数量限制大小")
	public void setFileCount(int paramInt) {
		this.fileCount = paramInt;
	}

	@WidgetTagAttribute(description = "是否允许多文件上传,默认为true")
	public void setEnableMulti(boolean paramBoolean) {
		this.enableMulti = paramBoolean;
	}

	public void setUrl(String paramString) {
		this.url = paramString;
	}

	public void setUrlParams(String paramString) {
		this.urlParams = paramString;
	}

	public String getButtonImg() {
		return this.buttonImg;
	}

	public void setButtonImg(String paramString) {
		this.buttonImg = paramString;
	}

	public String getUploadImg() {
		return this.uploadImg;
	}

	public void setUploadImg(String paramString) {
		this.uploadImg = paramString;
	}

	public String getRemoveImg() {
		return this.removeImg;
	}

	public void setRemoveImg(String paramString) {
		this.removeImg = paramString;
	}

	public String getHeight() {
		return this.height;
	}

	@WidgetTagAttribute(description = "设置浏览按钮的高度")
	public void setHeight(String paramInt) {
		this.height = paramInt;
	}

	public String getWidth() {
		return this.width;
	}

	@WidgetTagAttribute(description = "设置浏览按钮的宽度")
	public void setWidth(String paramInt) {
		this.width = paramInt;
	}
}